<?php

namespace App\Http\Controllers\Admin;

use App\Models\TestTemplate;

class TemplateController extends Controller
{
    /*
     * @api get /course-api/a/templates 测试模版列表
     * @version 0.0.1
     * @group admin
     *
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $this->validate($this->request, [
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $query = TestTemplate::query();
        $total = $query->count();
        $attachments = $query->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get(['id', 'name', 'description']);
        $items = [];
        if ($total) {
            foreach ($attachments as $attachment) {
                $items[] = [
                    'id' => $attachment->id,
                    'name' => $attachment->getName(),
                    'description' => $attachment->getDescription(),
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }



    /*
     * @api post /course-api/a/templates 新增测试模版
     * @version 0.0.1
     * @group admin
     *
     * @param body string name 课时名称
     * @param body string description 课时描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'name' => 'required|string',
            'description' => 'required|string',
        ]);
        $name = $this->request->input('name');
        $description = $this->request->input('description');

        /** @var TestTemplate $testTemplateObj */
        $testTemplateObj =  new TestTemplate();
        $testTemplateObj->setName($name);
        $testTemplateObj->setDescription($description);
        $testTemplateObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /course-api/a/test-templates/{templateId} 更新测试模版
     * @version 0.0.1
     * @group admin
     *
     * @param path int templateId 课程ID
     * @param body string name 课时名称
     * @param body string description 课时描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($templateId)
    {

        $this->request->merge(['id' => $templateId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            //'name' => 'required|string',
            'description' => 'required|string',
        ]);
        $name = $this->request->input('name');
        $description = $this->request->input('description');

        /** @var TestTemplate $testTemplateObj */
        $testTemplateObj = TestTemplate::find($templateId);
        //$testTemplateObj->setName($name);
        $testTemplateObj->setDescription($description);
        $testTemplateObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /course-api/a/test-templates/{templateId} 删除模版
     * @version 0.0.1
     * @group admin
     *
     * @param path int templateId 模版ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($templateId)
    {
        TestTemplate::destroy(intval($templateId));
        return $this->sendSuccessJson();
    }
}
